package top.suven.http.oauth.vo;

import org.apache.oltu.oauth2.common.domain.client.BasicClientInfo;
import org.apache.oltu.oauth2.common.message.types.GrantType;

import java.util.Date;


/**
 * 2018-08-01
 * <p/>
 *  OauthAccessToken 信息
 * 定义OAuth2 的客户端(client details)
 *  grant_type="authorization_code"
 *  DBTable: oauth_client_details
 *
 * @author suven.wang
 */

public class OauthClientDetails extends BasicClientInfo {

    public int status;
    /**
     * 客户端所拥有的资源ID(resource-id), 至少有一个,
     * 多个ID时使用逗号(,)分隔, 如:  os,mobile
     */
    private String resourceIds;

    private String scope;

    /**
     * 客户端所支持的授权模式(grant_type),
     * 至少一个, 多个值时使用逗号(,)分隔, 如: password,refresh_token
     *   AUTHORIZATION_CODE("authorization_code"),
     *     IMPLICIT("implicit"),
     *     PASSWORD("password"),
     *     REFRESH_TOKEN("refresh_token"),
     *     CLIENT_CREDENTIALS("client_credentials"),
     *     JWT_BEARER("urn:ietf:params:oauth:grant-type:jwt-bearer");
     */
    private String grantTypes = "authorization_code,implicit,password,refresh_token,client_credentials";


    /**
     * access_token 的有效时长, 单位: 秒.
     * 若不填或为空(null)则使用默认值: 12小时
     */
    private int accessTokenValidity;

    /**
     * refresh_token的 有效时长, 单位: 秒
     * 若不填或为空(null)则使用默认值: 30天
     */
    private int refreshTokenValidity;

    /**
     * 该 客户端是否为授信任的,
     * 若为信任的,, 则在 grant_type = authorization_code 时将跳过用户同意/授权 步骤
     */
    private boolean trust = false;

    /**
     * 逻辑删除的标识, true表示已经删除
     */
    private boolean archived = false;

    /**
     * 创建时间
     */
    private Date createTime = new Date();


    public OauthClientDetails() {
    }


    public String resourceIds() {
        return resourceIds;
    }

    public OauthClientDetails resourceIds(String resourceIds) {
        this.resourceIds = resourceIds;
        return this;
    }

    public String scope() {
        return scope;
    }

    public OauthClientDetails scope(String scope) {
        this.scope = scope;
        return this;
    }

    public String grantTypes() {
        return grantTypes;
    }

    public OauthClientDetails grantTypes(String grantTypes) {
        this.grantTypes = grantTypes;
        return this;
    }


    public Integer accessTokenValidity() {
        return accessTokenValidity;
    }

    public OauthClientDetails accessTokenValidity(Integer accessTokenValidity) {
        this.accessTokenValidity = accessTokenValidity;
        return this;
    }

    public Integer refreshTokenValidity() {
        return refreshTokenValidity;
    }

    public OauthClientDetails refreshTokenValidity(Integer refreshTokenValidity) {
        this.refreshTokenValidity = refreshTokenValidity;
        return this;
    }

    public boolean trust() {
        return trust;
    }

    public OauthClientDetails trusted(boolean trust) {
        this.trust = trust;
        return this;
    }

    public boolean archived() {
        return archived;
    }

    public OauthClientDetails archived(boolean archived) {
        this.archived = archived;
        return this;
    }

    public Date createTime() {
        return createTime;
    }

    public OauthClientDetails createTime(Date createTime) {
        this.createTime = createTime;
        return this;
    }

    public boolean supportRefreshToken() {
        return this.grantTypes != null && this.grantTypes.contains(GrantType.REFRESH_TOKEN.toString());
    }

    public int getStatus() {
        return status;
    }

    public OauthClientDetails setStatus(int status) {
        this.status = status;
        return this;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public boolean isTrust() {
        return trust;
    }

    public void setTrust(boolean trust) {
        this.trust = trust;
    }
}
